<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\modules\Cruise\models\Image;
use app\modules\Cruise\models\Cruise;
use yii\web\UploadedFile;
use PhpOffice\PhpWord\PhpWord;

class WordController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }


    public function actionWord(){
        $phpWord = new \PhpOffice\PhpWord\PhpWord();

        // Adding an empty Section to the document...
        $section = $phpWord->addSection();

        // Adding Text element to the Section having font styled by default...
        $phpWord->addTitleStyle('title', ['bold'=>true,'size'=>20]);
        $section->addTitle('恒信游轮模板',['title'] );
        
        //Add Table to the Word
        //$table_style = ['borderColor'=>'039ad3', 'border-size'=>5, 'cellMargin'=>50];
        //$table_first_row = ['bgColor'=>'66BBFF'];
        //$phpWord->addTableStyle('table1', $table_style, $table_first_row);
        //$table = $section->addTable('table1');
        //$table->addRow([30]);
        //$cell = $table->addCell(400);

        /*  Add Image To The Word*/
        $section->addImage('/srv/sites/cruise/web/img/word/3.png',
                                [
                                    'width'         => 100,
                                    'height'        => 100,
                                    'marginTop'     => -1,
                                    'marginLeft'    => -1,
                                    'wrappingStyle' => 'behind'
                                ]
                           );

        $header = $section->addHeader();
        //$footer = $section->addFooter();
        $header->addImage('/srv/sites/cruise/web/img/word/5.png',
                                [
                                    'width'         => 20,
                                    'height'        => 20,
                                    'marginTop'     => -1,
                                    'marginLeft'    => -1,
                                    'wrappingStyle' => 'behind'
                                ]
                           );
        $header->addText('韩国济州日本福冈之旅');
        
        // Adding Text element with font customized using explicitly created font style object...
        $fontStyle = new \PhpOffice\PhpWord\Style\Font();
        $fontStyle->setBold(true);
        $fontStyle->setName('Tahoma');
        $fontStyle->setSize(13);
        $myTextElement = $section->addText('恒信邮轮');
        $myTextElement->setFontStyle($fontStyle);
        
        // Saving the document as OOXML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $ret = $objWriter->save('/srv/sites/cruise/web/uploads/helloWorld.docx');
        var_dump($ret);
    }




}
